﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.cruxomatic.BusinessFacades;
using com.cruxomatic.views.Administration;
using com.cruxomatic.views.Enums;
using com.cruxomatic.DTOs;
using com.cruxomatic.logging;
using com.cruxomatic.logging.Enums;
using com.cruxomatic.exceptions;
using com.cruxomatic.security;

namespace com.cruxomatic.controllers.Administration
{
    public class ManageLocationsController
    {
        IManageLocations _view = null;
        public ManageLocationsController(IManageLocations view)
        {
            _view = view;
        }

        public void InitView(bool IsPostBack)
        {
            if (!IsPostBack)
            {
                CheckValidity();
                SwitchToListMode();
            }
        }

        private void CheckValidity()
        {
            ICruxSecurityManager cruxSecurityManager = CruxSecurityManagerFactory.GetSecurityManager();
            SessionData sessionData = new SessionData();
            UserInformation currentuser = sessionData.CurrentUser;
            if (!cruxSecurityManager.IsUserSuperAdminOrCompanyAdmin(currentuser, _view.CompanyID))
                NavigationController.OpenErrorPage("803");

        }

        public void OpenLocation(int LocationID)
        {
            LocationFacade locationFacade = new LocationFacade();
            LocationInformation locationInformation = new LocationInformation();
            locationInformation = locationFacade.GetLocation(LocationID);
            _view.CurrentLocation = locationInformation; 
            _view.FormMode = FormMode.AddEdit;
        }

        public void CreateLocation()
        {
            OpenLocation(0);
        }

        public void SaveLocation()
        {
            try
            {
                LocationFacade locationFacade = new LocationFacade();
                locationFacade.SaveLocation(_view.CurrentLocation);
                SwitchToListMode();
            }
            catch (CruxDatabaseException ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError(ex.ErrorNameOrCode, ex.Message);
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
            
        }

        public void SwitchToListMode()
        {
            _view.ErrorDetails = null;
            _view.CurrentLocation = new LocationInformation();
            LocationFacade locationFacade = new LocationFacade();
            _view.Locations = locationFacade.GetLocations(_view.CompanyID);
            _view.FormMode = FormMode.List;
        }

        public void CancelAddEdit()
        {
            SwitchToListMode();
        }

        public void DeleteLocation(int LocationID)
        {
            
            try
            {
                LocationFacade locationFacade = new LocationFacade();
                locationFacade.DeleteLocation(LocationID);
                SwitchToListMode();
            }
            catch (Exception ex)
            {
                ApplicationLogger.LogApplicationMessage(
                    ex.Message, ApplicationLogType.Database, ex.StackTrace);
                _view.ErrorDetails = CruxApplicationErrorManager.GetApplicationError("101", ex.Message);
            }
        }

        public void OpenSubLocationView(int LocationID)
        {
            NavigationController.OpenView("ManageSubLocations", LocationID);
        }

        public CompanyInformation GetCompanyForLocation(int CompanyID)
        {
            ICruxSecurityManager securityManager = CruxSecurityManagerFactory.GetSecurityManager();
            return securityManager.GetCompany(CompanyID);
        }

        public void RedirectToPreviousPage()
        {
            NavigationController.OpenView("ManageCompany", _view.CompanyID);
        }
    }
}
